package vn.edu.ptithcm.pim.util;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateTimeUtil {

	public static int compare2Days(Date date1, Date date2, String format) {
		if (date1 == null || date2 == null) {
			return 0;
		}
		SimpleDateFormat fmt = new SimpleDateFormat(format);
		int c = fmt.format(date1).compareToIgnoreCase(fmt.format(date2));
		return c;
	}

	public static String dateToString(Date date, String format) {
		if (date == null) {
			return "";
		}
		SimpleDateFormat fmt = new SimpleDateFormat(format);
		return fmt.format(date);
	}

	public static long getTimeFromMilliseconds(long mSec, char type) {
		long sec = (mSec / 1000);
		switch (type) {
		case 'm':
			return (sec / 60);
		case 'h':
			return (sec / (60 * 60));
		case 'd':
			return (sec / (60 * 60 * 24));
		default:
			return (sec / (60));
		}
	}

	public static long diffInHours(Date fromDate, Date toDate) {
		long msec = (toDate.getTime() - fromDate.getTime());
		if (msec <= 0) {
			msec = 0;
		}
		return getTimeFromMilliseconds(msec, 'h');
	}
}
